<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PayVendor extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($invoiceID, $vendorid, $vendor, $description, $duedate, $amount)
	{
		$data['invoiceID'] = $invoiceID;
		$data['vendorid'] = $vendorid;
		$data['vendor'] =  $vendor;
		$data['description'] =  $description;
		$data['duedate'] =  $duedate;
		$data['amount'] =  $amount;
		
		$this->load->view('v_payvendor', $data);
	}
	
	public function pay()
	{
		$invoiceID = $this->input->post('invoiceID', TRUE);
		$Vendor_ID = $this->input->post('vendorid', TRUE);
		$amount = $this->input->post('amount', TRUE);
		$vendor = $this->input->post('vendor', TRUE);
		
		$this->load->model('m_payvendor');
		
		$recordset = $this->m_payvendor->get_vendor_source($Vendor_ID);
		$results = $recordset->result();
		$vendorsource = $results[0]->Vendor_Source . '/billPaid';
		
		$postdata = http_build_query(
			array(
				'billstate' => 'PAID',
				'invoiceID' => $invoiceID,
				'amount' => $amount
			)
		);
		
		$json = NULL;

		if(function_exists("curl_init"))
		{
			$curl_handle = curl_init();
			curl_setopt ($curl_handle, CURLOPT_POST, TRUE);
			curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $postdata);
			curl_setopt ($curl_handle, CURLOPT_URL, $vendorsource);
			curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, TRUE);
			$json = curl_exec($curl_handle);
			curl_close($curl_handle);
		}
		else
		{
			$opts = array('http' =>
				array(
					'method'  => 'POST',
					'header'  => 'Content-type: application/x-www-form-urlencoded',
					'content' => $postdata
				)
			);
			
			$context  = stream_context_create($opts);

			$json = file_get_contents($url, false, $context);
		}

		$vendorObject = json_decode($json);
		
		$this->m_payvendor->add_transaction($invoiceID, $vendor);
		
		$message = "Test Email";
		
		//$this->load->library('email');

		//$this->email->clear();
		
		//$this->email->from('murtnowski@greenstreetadvisors.com','CECS492 Billing Service');
		//$this->email->to('murtnowski@gmail.com');
		//$this->email->subject('Your bill was paid!');
		//$this->email->message($message);
		//$this->email->send();

		$this->load->view('v_paid');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */